<?php

namespace App\Http\Controllers\Core;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class WebUploaderController extends Controller
{

    private function fpath($type){
        $fpath = [
            'image' => 'data/uploads/image/',
            'video' => 'data/uploads/video/',
            'files' => 'data/uploads/files/',
        ];
        if(array_key_exists($type, $fpath)){
            if(!file_exists( public_path($fpath[$type]) )){
                mkdir(public_path($fpath[$type]), 0777, true);
            }
            return $fpath[$type];
        }else{
            die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "保存路径错误"}, "id" : "0"}');
        }
    }

    /**
     * 图片上传
     */
    public function image(Request $request){
        $file = $request->file('file');//获取图片
        $allowed_extensions = ["png", "jpg", "jpeg", "gif"];
        if ($file->getClientOriginalExtension() && !in_array($file->getClientOriginalExtension(), $allowed_extensions)) {
            return response()->json(['status'=>403, 'msg'=> '只能上传 png | jpg | gif格式的图片', 'data' => '']);
        }
        $extension = $file->getClientOriginalExtension();
        $fileName = str_random(40).'.'.$extension;
        $fpath = $this->fpath('image');
        $file->move($fpath, $fileName);
        return response()->json(['jsonrpc'=>'2.0', 'id'=> '1', 'result' => $fpath.$fileName]);
    }


    public function baseHandle(){

    }

}
